package com.xindaoapp.happypet.shop.ui;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.GoodsAttr.AttrValue;
import com.xindaoapp.happypet.bean.MoreProduct;
import com.xindaoapp.happypet.bean.Product;
import com.xindaoapp.happypet.entry.CommentInfos;
import com.xindaoapp.happypet.entry.CommentInfos.Info.CommentInfo;
import com.xindaoapp.happypet.entry.GoodsInfos.GoodsInfo;

/**
 * 商品详情页面
 * 
 * @author xindaoapp
 * 
 */
public class ProductDetailAdapter extends BaseAdapter {
    Activity mContext;
    GoodsInfo mProductEntity;
    Product mProductInfos;
    CommentInfos mCommentInfos;
    int mTabIndex;

    public ProductDetailAdapter(int tabIndex, Activity mContext, GoodsInfo mProductEntity,
            Product mProductInfos2, CommentInfos commentInfos) {
        super();
        this.mContext = mContext;
        this.mProductEntity = mProductEntity;
        this.mProductInfos = mProductInfos2;
        this.mTabIndex = tabIndex;
        this.mCommentInfos = commentInfos;
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (mTabIndex == 0) {// 产品描述
            view = View.inflate(mContext, R.layout.fragment_shop_detail, null);
            WebView webview = ((WebView) view.findViewById(R.id.product_info));
            webview.getSettings().setDefaultTextEncodingName("UTF-8");

            List<CommentInfo> commentInfos = new ArrayList<CommentInfo>();
            TextView look_more_comment = (TextView) view.findViewById(R.id.look_more_comment);

            if (mCommentInfos.data != null && mCommentInfos.data.info != null) {
                if (mCommentInfos.data.info.size() < 3) {
                    commentInfos.addAll(mCommentInfos.data.info);
                } else {
                    commentInfos = mCommentInfos.data.info.subList(0, 3);
                }
                look_more_comment
                        .setText(String.format("查看全部%s条评价", mCommentInfos.data.info.size()));
            } else {
                look_more_comment.setVisibility(View.GONE);
            }

            // 添加3条评论
            ListView listView = (ListView) view.findViewById(R.id.comment_listview);
            listView.setAdapter(new ProductDetailCommentAdapter(mProductEntity.goods_id, mContext,
                    commentInfos, 20, R.layout.list_item_product_comment, R.layout.item_loading));
            // AutoMeasureGridView gridView =
            // (AutoMeasureGridView) view.findViewById(R.id.recommend_gridview);
            // gridView.setAdapter(new RecommendAdapter(mProductInfos.introduce_goods));

            if (!TextUtils.isEmpty(mProductEntity.goods_desc)) {
                webview.loadData(mProductEntity.goods_desc, "text/html; charset=UTF-8", null);
            }

            look_more_comment.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((ProductDetailActivity) mContext).switchViewPager(2);
                }
            });
        } else if (mTabIndex == 1) {// 产品规格
            view = View.inflate(mContext, R.layout.layout_product_standard, null);
            if (mProductInfos != null) {
                ((TextView) view.findViewById(R.id.standard)).setText(String.format("%sKG",
                        mProductInfos.goods_weight));

                // 通用属性
                if (mProductInfos.goods_attr.size() > 0) {
                    StringBuffer sb = new StringBuffer();
                    List<AttrValue> attrValues = mProductInfos.goods_attr.get(0).attr_value;
                    for (int i = 0; i < attrValues.size(); i++) {
                        sb.append(attrValues.get(i).attr_value + " ");
                    }
                    ((TextView) view.findViewById(R.id.general_attribute)).setText(sb.toString());
                }
            }

        }
        return view;
    }

    @SuppressLint("NewApi")
    private void loadHtml(WebView webView) {

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.getSettings().setSupportZoom(true);
        // webView.getSettings().setDisplayZoomControls(false);
        // 缩放按钮
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setDisplayZoomControls(false);


    }

    /**
     * 评论适配器，详情里面的评论列表
     * 
     * （最多三条）
     * 
     * @author xindaoapp
     * 
     */
    class CommentAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return null;
        }

    }

    /**
     * 详情里面的商品推荐
     * 
     * @author xindaoapp
     * 
     */
    class RecommendAdapter extends BaseAdapter {
        List<MoreProduct> list;

        public RecommendAdapter(List<MoreProduct> list) {
            super();
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(mContext, R.layout.item_product_recommend, null);
                holder.icon = (ImageView) view.findViewById(R.id.icon);
                holder.price = (TextView) view.findViewById(R.id.price);
                holder.title = (TextView) view.findViewById(R.id.title);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            MoreProduct info = list.get(position);

            ImageLoader.getInstance().displayImage(info.goods_thumb, holder.icon);
            holder.price.setText(info.shop_price);
            holder.title.setText(info.goods_name);
            return view;
        }

        class ViewHolder {
            ImageView icon;
            TextView title;
            TextView price;
        }

    }

}
